package com.wurao.listener;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.wurao.dao.BlogDao;
import com.wurao.entity.Blog;
import com.wurao.entity.BlogType;
import com.wurao.entity.Blogger;
import com.wurao.entity.Link;
import com.wurao.service.BlogService;
import com.wurao.service.BlogTypeService;
import com.wurao.service.BloggerService;
import com.wurao.service.LinkService;

/**
 * 初始化就把基础信息存入session中
 * @author xxxx
 *
 */
@Component
public class InitComponent implements ServletContextListener,ApplicationContextAware {
	private static ApplicationContext applicationContext;
	/**
	 * 销毁
	 */
	public void contextDestroyed(ServletContextEvent sce) {
	
	}

	/**
	 * 初始化
	 */
	public void contextInitialized(ServletContextEvent sce) {
		ServletContext  application=sce.getServletContext();
		BloggerService bs= (BloggerService) applicationContext.getBean("bloggerService");
		Blogger blogger=bs.find(1);//获取博主信息
		blogger.setPassword(null);
		application.setAttribute("blogger",blogger);

		LinkService ls=(LinkService) applicationContext.getBean("linkService");
		Map<String,Object> map=new HashMap<String, Object>();
		List<Link> linkList=ls.listLink(null);//获取友情链接集合 （由于链接不多，不需要分页，map不需传值）
		application.setAttribute("linkList",linkList);
		
		BlogTypeService bts=(BlogTypeService) applicationContext.getBean("blogTypeService");
		List<BlogType> blogTypeCountList=bts.countList();//查询日志类别
		application.setAttribute("blogTypeCountList",blogTypeCountList);
	
		BlogService blogService=(BlogService) applicationContext.getBean("blogService");
		List<Blog> blogCountList=blogService.countList();//查询日志日期
		application.setAttribute("blogCountList",blogCountList);
		
		List<Blog> blogTopCountList=blogService.topCountList();//查询热门文章
		application.setAttribute("blogTopCountList",blogTopCountList);

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			application.setAttribute("years",dayComparePrecise("2016-05-30", df.format(new Date())));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext=applicationContext;
	}

	/**
	 * 计算2个日期之间相差的  以年、月、日为单位，各自计算结果是多少
	 * 比如：2011-02-02 到  2017-03-02
	 *                                以年为单位相差为：6年
	 *                                以月为单位相差为：73个月
	 *                                以日为单位相差为：2220天
	 * @param fromDate
	 * @param toDate
	 * @return
	 */
	public static String dayComparePrecise(String fromDate, String toDate){
		Period period = Period.between(LocalDate.parse(fromDate), LocalDate.parse(toDate));
		StringBuffer sb = new StringBuffer();
		//sb.append(period.getYears()).append(",").append(period.getMonths()).append(",").append(period.getDays());
		sb.append(period.getYears()).append("年").append(period.getMonths()).append("月");
		return sb.toString();
	}
}
